<?php

namespace App\Models;

use App\Engine\BaseModel;
use Illuminate\Database\Eloquent\Model;

class Banner extends BaseModel
{
    protected $fillable=['name','img_id','type','value','banner_type','sort'];
    public function img()
    {
        return $this->belongsTo('App\Models\Image','img_id','id');
    }

    public static function get_data($get)
    {
        $data=self::with('img')->where($get)->orderBy('sort','asc')->get()->map(function ($item){
            $item['title']=$item['name'];
            if($item['img'])
                $item['image']=Sys::getValue('api_url').$item['img']['url'];
            return $item;
        });
        return $data;

    }


    /**获取广告图
     * @param string $banner_type
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function get_banner($banner_type="")
    {
        if($banner_type=="")
            return self::with('img')->get();
        else
            return  self::with('img')->where('banner_type',$banner_type)->get();
    }





    /**插入广告
     * @param $data
     * @return mixed
     */
    public static function insert_banner($data)
    {
        return self::create($data);
    }

    /**更新广告
     * @param $data
     * @return mixed
     */
    public static function update_banner($data)
    {
        return self::find($data['id'])->update($data);
    }

    /**删除广告信息
     * @param $id
     * @return mixed
     */
    public static function del_banner($id)
    {
        return self::where('id',$id)->delete();
    }
}
